package no.ntnu.item.ttm4115.termproject.configurationagentgm;

import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import com.thoughtworks.xstream.XStream;

import no.ntnu.item.ttm4115.termproject.FetchConfiguration;
import no.ntnu.item.ttm4115.termproject.InformGroupAboutDiscussion;
import no.ntnu.item.ttm4115.termproject.SendGameConfigToTeacher;
import no.ntnu.item.ttm4115.termproject.extrasagent.Game;
import no.ntnu.item.ttm4115.termproject.extrasagent.Group;
import no.ntnu.item.ttm4115.termproject.extrasagent.Player;
import no.ntnu.item.ttm4115.termproject.extrasagent.Treasures;
import no.ntnu.item.ttm4115.termproject.SendGameConfig2;


public class ConfigurationAgentGMActions extends Object {

	public static void initialize(ConfigurationAgentGMSM asm){
	    // TODO edit action statements
	}

	
	public static void uploadConfig(FetchConfiguration signal, ConfigurationAgentGMSM asm){
			asm.teacherAddress = signal.teacherAddress;
			
		    XStream xstream = new XStream();
			xstream.setClassLoader(new Game().getClass().getClassLoader());
			
			xstream.alias("treasures", Treasures.class);
			xstream.alias("game",Game.class);
			xstream.setMode(XStream.XPATH_ABSOLUTE_REFERENCES);
			boolean hash = false;
			boolean vector = false;
			for (int i = 0; i < asm.configuration.size(); i++) {
				String treasure = xstream.toXML((Treasures)asm.configuration.elementAt(i));
				if(i == (asm.configuration.size()-1)){
					vector = true;
					System.out.println("her settes VECTOR til TRUE!!!!");
				}
				asm.sendMessage(new SendGameConfigToTeacher(treasure,vector), asm.teacherAddress);
			}
			
			Iterator<Entry<String, Game>> iterator = signal.games.entrySet().iterator();
			System.out.println(signal.games.size());
		    while(iterator.hasNext()){	
		    		
		    	 Map.Entry<String, Game> me = (Map.Entry<String, Game>) iterator.next();
		    	 String name = me.getKey();
		    	 System.out.println(me.getKey());
		    	 String game = xstream.toXML(me.getValue());
		    	 System.out.println(me.getValue());
		    	 if(!iterator.hasNext()){
		    		 hash = true;
		    		 System.out.println("Her blir HASH satt til true");
		    	 }
				 asm.sendMessage(new SendGameConfig2(name,game,hash), asm.teacherAddress);
					
			 }
	}



}
